/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package com.scm.hibernate.entities;

import com.scm.hibernate.utilities.HibernateUtil;
import java.util.List;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
import org.hibernate.Query;
import org.hibernate.Session;

/**
 *
 * @author unnati
 */
@Table(name = "branches")
@Entity
public class Branch extends AbstractEntity
{
    @GeneratedValue
    
    @Column(name = "id")
    @Id
    protected Long id;
    
    @Column(name = "name")
    protected String name;
    
    @Column(name = "addedby")
    protected Long addedby;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Long getAddedby() {
        return addedby;
    }

    public void setAddedby(Long addedby) {
        this.addedby = addedby;
    }

    
   
    
     public Branch getByName() {
        Branch br = null;
        try {
            Session session = HibernateUtil.getSessionFactory().openSession();
             Query query = session.createQuery("from Branch where name= '"+this.getName()+"' ");
            List<Branch> li=query.list();
            
            if(li.size()==1)
            {
              br =li.get(0);
            }
            
            
        } catch (Exception e) {
            e.printStackTrace();
        }
        return br;
        
    }

     public static List<Branch> getallbranches() {
        List<Branch> li = null;
        try {
            Session s = HibernateUtil.getSessionFactory().openSession();

            Query q = s.createQuery("from Branch");

            li = q.list();
            System.out.println("Get All branches");

        } catch (Exception e) {
               e.printStackTrace();
        }
        return li;
    }
}
